var SCRIBEFIRE_PREFS_OBSERVER = {
    load : function () {
        this.prefs = Components.classes["@mozilla.org/preferences-service;1"].getService(Components.interfaces.nsIPrefService).getBranch("performancing.");	
		this.prefs.QueryInterface(Components.interfaces.nsIPrefBranch2);
		this.prefs.addObserver("", this, false);
    },
    
    observe : function(subject, topic, data) {
    	if (topic != "nsPref:changed") {
    		return;
    	}

        var wm = Components.classes["@mozilla.org/appshell/window-mediator;1"]
                           .getService(Components.interfaces.nsIWindowMediator);

    	switch(data) {
    	    case "display.onbottom":
                // alert("Changing the position of ScribeFire in split-screen mode requires a browser restart.");
    	    break;
    	    case "settings.usecss":
    	        this.execFunction(
    	            function (win) {
    	                win.performancingEditor.setUseCSS(SCRIBEFIRE_PREFS_OBSERVER.prefs.getBoolPref("settings.usecss"));
    	            }
    	        );
    	    break;
    	    case "display.hideContextMenu":
    	        var enumerator = wm.getEnumerator("navigator:browser");
    	        
    	        while (enumerator.hasMoreElements()) {
                    var win = enumerator.getNext();
                    
                    try {
                        win.gperformancing.showHideCM();
                    } catch (e) {
                    }
                }
    	    break;
    	    case "extra.inserttechnorati":
        	    this.execFunction(
    	            function (win) {
    	                win.performancingUI.initTechnorati();
    	            }
    	        );
    	    break;
    	    case "display.enablertl":
        	    this.execFunction(
    	            function (win) {
    	                win.performancingUI.onTextBoxLoad2();
    	            }
    	        );
    	    break;
    	    case "display.autosave":
        	    this.execFunction(
    	            function (win) {
    	                if (SCRIBEFIRE_PREFS_OBSERVER.prefs.getBoolPref("display.autosave")) {
                            win.performancingEditor.startAutoSave();
                        }
                        else {
                            win.performancingEditor.stopAutoSave();
                        }
    	            }
    	        );
    	    break;
    	}
    },
    
    execFunction : function (callback) {
        var windowMediator = Components.classes["@mozilla.org/appshell/window-mediator;1"].getService(Components.interfaces.nsIWindowMediator);
		var enumerator = windowMediator.getEnumerator("");

		while (enumerator.hasMoreElements()) {
			var win = enumerator.getNext();

			if (win.location == "chrome://scribefire/content/scribefire.xul") {
			    callback(win);
			}
			else {
				var splitScreen = win.document.getElementById("perFormancingMidasFrame");

				if (splitScreen && splitScreen.getAttribute("src") == 'chrome://scribefire/content/scribefire.xul') {
                    var thewin = win.document.getElementById("perFormancingMidasFrame").contentWindow;
				    callback(thewin);
				}
			}
		}
    
    	var browserEnumerator = windowMediator.getEnumerator("navigator:browser");

		// Check all of the tabs for ScribeFire
		while (browserEnumerator.hasMoreElements()) {
			var browserInstance = browserEnumerator.getNext().getWebBrowser();

			// Check each tab of this browser instance
			var numTabs = browserInstance.tabContainer.childNodes.length;
		    
		    for(var index = 0; index < numTabs; index++) {
				var currentBrowser = browserInstance.getBrowserAtIndex(index);
				
				if (currentBrowser.currentURI.spec == 'chrome://scribefire/content/scribefire.xul') {
					browserInstance.selectedTab = browserInstance.tabContainer.childNodes[index];

					if (currentBrowser.contentWindow.wrappedJSObject) {
					    callback(currentBrowser.contentWindow.wrappedJSObject);
					}
				}
			}
		}
    }
};

function scribefire_observer_load() {
    SCRIBEFIRE_PREFS_OBSERVER.load();
}

addEventListener("load", scribefire_observer_load, false);
addEventListener("unload", function () { removeEventListener("load", scribefire_observer_load, false); }, false);